package com.qs.controller;

import com.github.pagehelper.PageInfo;
import com.qs.ConStants;
import com.qs.commen.Response;
import com.qs.pojo.SysRole;
import com.qss.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @user HP
 * @email 1460961774@qq.com
 * @Date 2021-09-28 11:14
 * @packageName com.qs.controller
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService service;
    @GetMapping("/toDate")
    public String toRole(){
        return "role/data";
    }
    @PostMapping("data")
    @ResponseBody
    public Response todata(SysRole role, @RequestParam(required = true,defaultValue = "1") Integer pageNum, @RequestParam(required = true,defaultValue = ConStants.DEFAULTE_PAGE) Integer pageSize){
        PageInfo<SysRole> sysRolePageInfo = service.queryRoleByPage(role, pageNum, pageSize);
        return new Response(200,sysRolePageInfo);
    }
    //跳转新增
    @GetMapping("toEdit")
    public  String toEdit(SysRole role, Model model){
        if (role!=null&&role.getRoleId()!=null){
            SysRole sysRole = service.queryById(role.getRoleId());
            model.addAttribute("roles",sysRole);
        }
        return "role/edit";
    }
    //删除
    @PostMapping("/save")
    @ResponseBody
    public Response save(SysRole role){
        boolean status=service.savaRole(role);
        if (status){
            return Response.success("保存成功");
        }
        return new Response(400,"操作失败");
    }
    //新增保存
    @DeleteMapping("/deletes")
    @ResponseBody
    public Response delete(Integer[] ids){

        boolean b = service.delRoles(ids);
        if (b){
            return Response.success("删除成功");
        }
        return Response.error();

    }

    //授权
    @GetMapping ("toPermission")
    public String  toPer(SysRole role,Model model){
        model.addAttribute("role",role);
        return "role/permission";
    }
    @PostMapping("savePermission")
    @ResponseBody
    public Response savePermission(SysRole role,@RequestParam(name = "ids[]") Integer[] ids){
        boolean status = service.saveRolePermission(role,ids);
        if(status)return Response.success("授权成功");
        return new Response(400,"操作失败");
    }

    @PostMapping("checkRoleName")
    @ResponseBody
    public  boolean check(SysRole role){

     SysRole  role1 = service.queryRoleByName(role.getRoleName());
        if (role1 == null) {
            return true;
        }
        return false;
    }


}
